<?php 

class UserController extends BaseController
{
	// gets view for register page
	public function getCreate()
	{
		return View::make('user.register');
	}
	//gets the view for login page 
	public function getLogin()
	{
		return View::make('user.login');
	}
	
	public function getLogout()
	{
	 Auth::logout();
    return Redirect::route('home');
	}
	
	public function postCreate()
	{
		$validate = Validator::make(Input::all(), array(
			'username' => 'required|unique:users|min:4',
			'pass1' => 'required|min:6',
			'pass2' => 'required|same:pass1',
			'firstname' => 'required',
			'lastname' => 'required',
			'middlename' => 'required',
			'address' => 'required',
			'birthday' => 'required',
			'contact' => 'required|numeric'
		));
		
		
		if ($validate->fails())
		{
			return Redirect::route('getCreate')->withErrors($validate)->withInput();
		}
		else
		{
			
			$user = new User();
			$user->username = Input::get('username');
			$user->password = Hash::make(Input::get('pass1'));
			$user->firstname = Input::get('firstname');
			$user->lastname = Input::get('lastname');
			$user->middlename = Input::get('middlename');
			$user->address = Input::get('address');
			$user->birthday = Input::get('birthday');
				$user->contact = Input::get('contact');
				$user->email = Input::get('email');
			if($user->save())
			{
				return Redirect::route('home')->with('success', 'You have been registered to the system! Welcome to SAMS!');
			}
		else
		{
			return Redirect::route('home')->with('fail', 'An error occured while creating the user. Please double check your inputs and try again.');
		}
		}
	}
	public function postLogin()
	{
		$validator =  Validator::make(Input::all(), array(
			'username' => 'required',
			'pass1' => 'required'
		
		));
		if($validator->fails())
		{
			return Redirect::route('getLogin')->withErrors($validator)->withInput();
		}
		else
		{
			$remember = (Input::has('remember')) ? true : false;
			
			$auth = Auth::attempt(array(
				'username' => Input::get('username'),
				'password' => Input::get('pass1')
			), $remember);
			
			if($auth)
			{
				return Redirect::intended('/');
			}
			else
			{
					return Redirect::route('getLogin')->with('fail','You entered the wrong login credentials. Please try again.');
			}
		}
	}
}